package demo.factor.Ifactor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import cn.edu.nuc.onlinestore.model.Admin;
import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Client;
import cn.edu.nuc.onlinestore.model.Goods;
import demo.factor.fac;


public class Ifac implements fac {

	@SuppressWarnings("unchecked")
	@Override
	public Map<Client, Integer> readAllUser() {
		Map<Client, Integer> newStaff=null;
		File f=new File("D:/store/user/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try
		(
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:/store/user/ClientList.dat"));           
		){
		newStaff = (Map<Client, Integer>)in.readObject();
		return newStaff;
		}catch(Exception e){
			newStaff=new HashMap<>();
			e.printStackTrace();
		}
		return newStaff;
	}

	@Override
	public boolean writeAllUser(Map<Client, Integer> map) {

		File f=new File("D:/store/user/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try(
	        	ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/store/user/ClientList.dat")); 
	       ){
	        out.writeObject(map); //!!!!!!这个方法可以写集合、数组  
	        return true;
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
		return false;
	}

	@Override
	public Admin readAdmin() {
		Admin admin=null;
		File f=new File("D:/store/admin/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try
		(
		InputStream in=new FileInputStream(new File("D:/store/admin/Admin.dat"));
		ObjectInputStream in1=new ObjectInputStream(in);	
		){
			admin=(Admin)in1.readObject();
			return admin;
		}catch(Exception e){
			System.err.println("好像找不到Admin，或未知错误");
			e.printStackTrace();
		}
		return admin;		
	}

	@Override
	public boolean writeAllGoods(Set<Goods> list) {
		File f=new File("D:/store/goods/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try(
	        	ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/store/goods/GoodsList.dat")); 
	       ){
	        out.writeObject(list); //!!!!!!这个方法可以写集合、数组  
	        return true;
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<Goods> readAllGoods() {
		Set<Goods> list=null;
		File f=new File("D:/store/goods/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try
		(
		InputStream in=new FileInputStream(new File("D:/store/goods/GoodsList.dat"));
		ObjectInputStream in1=new ObjectInputStream(in);	
		){
			list=(Set<Goods>)in1.readObject();
			return list;
		}catch(Exception e){
			list=new HashSet<>();
			System.err.println("好像找不到Goods，或未知错误");
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Map<Client, Carts> readAllCarts() {
		Map<Client, Carts> newStaff=null;
		File f=new File("D:/store/carts/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try
		(
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:/store/carts/CartsList.dat"));           
		){
		newStaff = (Map<Client, Carts>)in.readObject();
		return newStaff;
		}catch(Exception e){
			newStaff=new HashMap<>();
			System.err.println("D:/store/carts/CartsList.dat为空");
			e.printStackTrace();
		}
		return newStaff;
	}

	@Override
	public boolean writeAllCarts(Map<Client, Carts> map) {
		File f=new File("D:/store/carts/");
		if(!f.exists())
		{
			f.mkdirs();
		}
		try(
	        	ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:/store/carts/CartsList.dat")); 
	       ){
	        out.writeObject(map); //!!!!!!这个方法可以写集合、数组  
	        return true;
	        }catch(Exception e){
	        	e.printStackTrace();
	        }
		return false;
	}	
	
	
}
